//
//  TabBarViewController.swift
//  CMCCIOTSwift
//

//  Copyright © 2017年 isoftstone. All rights reserved.
//

import UIKit

class TabBarViewController: UITabBarController {


    override func viewDidLoad() {
        super.viewDidLoad()


    
    let tabBarWidth : CGFloat = 64
    let tabBarHeight : CGFloat = 49
    let tabBarViewHeight : CGFloat = 60
    
    var tabButtons = [UIButton]()
    var imgArr = ["homepage_ic_aqdc","homepage_ic_aqdc","homepage_ic_aqdc"]
    var imgSelArr = ["homepage_ic_aqdc","homepage_ic_aqdc","homepage_ic_aqdc"]

    override func viewDidLoad() {
        super.viewDidLoad()
       

        
        self .customTabBar()
        self.initControllers()
        

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func customTabBar() -> Void
    {
        let tabBarOffsetX = UIScreen_Width/3
        let tabBarX = tabBarOffsetX/2 - tabBarWidth/2
        let tabBarY = tabBarViewHeight/2 - tabBarHeight/2
        let tabBarView = UIView(frame: CGRect(x:0, y:UIScreen_Height - tabBarViewHeight, width:UIScreen_Width, height:tabBarViewHeight))
        tabBarView.backgroundColor = UIColor.black
        self.view.addSubview(tabBarView)
       
        
        for index in 0..<imgArr.count
        {
            let tabBar_X = (CGFloat)(index) * tabBarOffsetX
            let btn = UIButton(frame: CGRect(x:(CGFloat)(tabBarX + tabBar_X), y:(CGFloat)(tabBarY), width:tabBarWidth, height:tabBarHeight))
            if index == 0 {
                let frame = CGRect(x: btn.frame.origin.x, y: btn.frame.origin.y, width: btn.frame.size.width-10, height: btn.frame.size.height)
                btn.frame = frame;
                
            }else if index == 1{
                let frame = CGRect(x: btn.frame.origin.x - 10, y: btn.frame.origin.y, width: btn.frame.size.width + 20, height: btn.frame.size.height)
                btn.frame = frame
            }else if index == 2{
                let frame = CGRect(x: btn.frame.origin.x+10, y: btn.frame.origin.y, width: btn.frame.size.width-10, height: btn.frame.size.height)
                btn.frame = frame;
            }
            if(index == 0)
            {
                btn.setBackgroundImage(UIImage(named: imgSelArr[index]), for: UIControlState.normal)
            }else{
                btn.setBackgroundImage(UIImage(named: imgArr[index]), for: UIControlState.normal)
            }
            
            btn.tag = index + 100
            btn.addTarget(self, action: #selector(tabAction(obj:)), for: UIControlEvents.touchUpInside)
            
            tabBarView.addSubview(btn)
            tabButtons.append(btn)
        }
        
    }
    
    func tabAction(obj: UIButton) -> Void
    {
        let indexSel = obj.tag - 100
        self.selectedIndex = indexSel
        for index in 0..<tabButtons.count
        {
            if(index == indexSel)
            {
                
                tabButtons[indexSel].setBackgroundImage(UIImage(named: imgSelArr[indexSel]), for: UIControlState.normal)
            }else{
                tabButtons[index].setBackgroundImage(UIImage(named: imgArr[index]), for: UIControlState.normal)
            }
        }
    }
    
    func initControllers() -> Void
    {
        let firstTabView = FirstViewController()
        let secondTabView = FirstViewController()
        let thirdTabView = FirstViewController()
        
        var viewArr = [firstTabView, secondTabView, thirdTabView]
        var viewCtlArr = [UIViewController]()
        for index in 0..<viewArr.count
        {
            if(index != 2)
            {
                let navController = UINavigationController(rootViewController: viewArr[index])
                viewCtlArr.append(navController)
            }else{
                viewCtlArr.append(viewArr[index])
            }
        }
        
        self.viewControllers = viewCtlArr
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    func loadViewControllers() {
        
    }


}
